﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;
using System.Globalization;

namespace SPNetworkTopology
{
    class VisioEvents
    {
        /// <summary>
        /// Visio ribbon instance
        /// </summary>
        private SharepointNetworkTopologyRibbon visRibbon = null;

        /// <summary>
        /// Visio document before close - event
        /// </summary>
        /// <param name="Doc">Visio Document</param>
        public void visApplication_BeforeDocumentClose(Visio.Document visioDocument)
        {
            if (!IsSharepointNetworkTopologyTemplate(visioDocument))
            {
                return;
            }

            //Check whether the visio document exists in our repository
            if (VisioObjects.visDocumentList.Contains(visioDocument))
            {
                visRibbon = null;
                if (VisioObjects.visDocRibbonCollection.TryGetValue(visioDocument, out visRibbon))
                {                    
                    visioDocument.Application.UnregisterRibbonX(visRibbon, visioDocument);

                    //Free the objects
                    VisioObjects.visDocRibbonCollection.Remove(visioDocument);
                    VisioObjects.visDocumentList.Remove(visioDocument);
                }
            }
        }

        /// <summary>
        /// Visio document created/opened event
        /// </summary>
        /// <param name="visioDocument">Visio Document</param>
        public void visApplication_DocumentCreatedOpened(Visio.Document visioDocument)
        { 
            if (!IsSharepointNetworkTopologyTemplate(visioDocument))
            {
                return;
            }

            bool docSavedState = visioDocument.Saved;

            if (!VisioObjects.visDocumentList.Contains(visioDocument))
            {
                visRibbon = new SharepointNetworkTopologyRibbon();

                //Register the fluent ribbon UI for this Add-In
                visioDocument.Application.RegisterRibbonX(visRibbon, visioDocument, Visio.VisRibbonXModes.visRXModeDrawing, "SharePoint Network Topology");
                

                //Update the collection
                VisioObjects.visDocumentList.Add(visioDocument);
                VisioObjects.visDocRibbonCollection.Add(visioDocument, visRibbon);                
            }

            // reset the saved state of the document after we have done our work
            visioDocument.Saved = docSavedState;
        }

        /// <summary>
        /// Check for custom template
        /// </summary>
        /// <param name="visioDocument"> Visio Document</param>
        /// <returns>custom template existence - bool</returns>
        private bool IsSharepointNetworkTopologyTemplate(Visio.Document visioDocument)
        {
            // get the document name
            string fullName = visioDocument.FullName.ToLower(CultureInfo.CurrentCulture);

            if (fullName.EndsWith(".vss") ||
                fullName.EndsWith(".vsx") ||
                visioDocument.Type == Visio.VisDocumentTypes.visTypeStencil)
            {
                // do not create a document wrapper for this document
                return false;
            }                      

            if (!Convert.ToBoolean((short)visioDocument.DocumentSheet.get_CellExistsU(
                                  "User.IsSharePointNetworkTopologyTemplate", (short)Visio.VisExistsFlags.visExistsAnywhere)))
            {
                // Consider documents created using addin template only
                return false;
            }

            return true;
        }
    }
}
